package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.NurseGroupPercentages;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.NurseGroupPercentagesDto;
import com.blueocean_health.care.management.service.NurseGroupPercentagesService;
import com.github.pagehelper.PageInfo;

/**
 * 提成管理  2018年5月14日18:59:30
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/nurse/group/percentages")
public class NurseGroupPercentagesController {
	
	@Resource
	private NurseGroupPercentagesService percentagesService;

	/**
	 * 提成管理列表  2018年5月16日14:51:13
	 * @param groupPercentages
	 * @return
	 */
	@RequestMapping("list")
	public SysResult percentagesList(@RequestBody Map<String,String> map){
		PageInfo<NurseGroupPercentagesDto> percentagesList = percentagesService.percentagesList(map);
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> pageMap = new HashMap<>();
		resultMap.put("list", percentagesList.getList());
		pageMap.put("total", percentagesList.getTotal());
		resultMap.put("pageInfo", pageMap);
		return SysResult.ok(0, "success",resultMap);
	}
	/**
	 * 提成管理 编辑护工小组提成信息  2018年5月14日19:02:34
	 * @param groupPercentages
	 * @return
	 */
	@RequestMapping("modify")
	public SysResult modifyGroupPercentages(@RequestBody NurseGroupPercentages groupPercentages){
		return percentagesService.modifyGroupPercentages(groupPercentages);
	}
	
	/**
	 * 导出Excel 订单流水列表 2017年8月25日15:23:19
	 * @param map
	 * @return
	 */
	@RequestMapping("list/excel")
	public void percentagesListExcel(HttpServletRequest request, HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			
			map.put("groupId", request.getParameter("groupId"));
			map.put("searchKey", request.getParameter("searchKey"));
			List<NurseGroupPercentagesDto> percentagesListExcel = percentagesService.percentagesListExcel(map);
			String finalFileName = URLEncoder.encode("提成管理_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, percentagesListExcel, NurseGroupPercentagesDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
